package com.cstz.hb.manage.controller;

import com.alibaba.fastjson.JSON;
import com.cstz.enums.DeptTypeEnum;
import com.cstz.enums.DictTypeEnum;
import com.cstz.hb.manage.domain.AppUser;
import com.cstz.hb.manage.domain.SysDept;
import com.cstz.hb.manage.domain.SysQuestion;
import com.cstz.hb.manage.service.AppUserService;
import com.cstz.hb.manage.service.SysDeptService;
import com.cstz.hb.manage.service.SysQuestionService;
import com.cstz.hb.manage.utils.ExportExcelUtil;
import com.cstz.ydshop.common.annotation.Log;
import com.cstz.ydshop.common.controller.BaseController;
import com.cstz.ydshop.common.domain.DictDO;
import com.cstz.ydshop.common.service.DictService;
import com.cstz.ydshop.common.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/web/quest")
@Controller
@Slf4j
public class AppQuestController extends BaseController {

    private String prefix="hbManage/question"  ;

    @Autowired
    private DictService dictService;

    @Autowired
    private SysQuestionService sysQuestionService;

	@GetMapping("")
	String quest(Model model) {
        List<DictDO> dicts = dictService.listType(DictTypeEnum.问题类型.getIndex());
        model.addAttribute("dicts",dicts);
		return prefix + "/quest";
	}

    @GetMapping("/list")
	@ResponseBody
	PageUtils list(@RequestParam Map<String, Object> params) {
        params.put("userId",getUserId());
		Query query = new Query(params);
		List<SysQuestion> sysQuestions = sysQuestionService.selectQuestions(query);
		int total = sysQuestionService.count(query);
		PageUtils pageUtil = new PageUtils(sysQuestions, total);
		return pageUtil;
	}

    /**
     * 导出问题excel
     * @param sysQuestion
     * @param request
     * @param response
     */
	@GetMapping("/export")
    public void export(@RequestParam Map<String, Object> params, HttpServletRequest request, HttpServletResponse response){
        OutputStream out = null;
        try{
            response.reset();
            out = response.getOutputStream();
            params.put("userId",getUserId());
            List<SysQuestion> sysQuestions = sysQuestionService.exportData(params);
            String fileName = "问题列表记录_"+DateUtil.format(new Date(),"yyyy年MM月dd日HH时mm分ss秒");
            String agent = request.getHeader("USER-AGENT");
            try{
                if (agent.contains("MSIE") || agent.contains("Trident")) {
                    fileName = URLEncoder.encode(fileName, "UTF-8");
                } else {
                    //非IE浏览器的处理：
                    fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
                }
            }catch (Exception e){
                log.error("文件名转码失败,",e.getMessage());
            }
            fileName = fileName+".xls";
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.flushBuffer();
            ExportExcelUtil.exportExcelData(sysQuestions,out);
        }catch (Exception e){
            log.error("导出问题列表出错,",e.getMessage());
        }finally {
            try{
                if(null!=out){
                    out.flush();
                    out.close();
                }
            }catch (Exception e){}
        }
    }

}
